package com.lanyou.cook.entitylistener.domain;

import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;

import org.apache.commons.lang3.StringUtils;
import org.pentaho.di.core.encryption.Encr;
import org.pentaho.di.core.encryption.KettleTwoWayPasswordEncoder;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.datadictionary.UserLogOpt;
import com.lanyou.cook.entity.domain.DBlink;
import com.lanyou.cook.service.UserLogService;
import com.lanyou.cook.util.RSAUtil;

public class DBlinkEntityListenerForLog {
	
	

	@PrePersist
	public void prePersist(DBlink target) {	
//		String password = target.getDestPassword();
//		try {
//			// 进行RSA解密
//		password = RSAUtil.decryptByPrivateKey(target.getDestPassword(), Constants.PRIVATE_KEY_BASE64);
//		target.setDestPassword(password);
//		} catch (Exception e) {
//
//		}

//		if(StringUtils.isEmpty(password))
//	    throw new RuntimeException("Password is invalid");
//		// 原密码未经过加密，则此时加密
//		if(!password.startsWith(KettleTwoWayPasswordEncoder.PASSWORD_ENCRYPTED_PREFIX)){
//			String encryptedPassword = Encr.encryptPasswordIfNotUsingVariables(password);
//			target.setDestPassword(encryptedPassword);
//		}
		String content = String.format("新增DBlink“%s”", target.getName());
		UserLogService.getInstance().save(UserLogOpt.MaintenanceDBlink, null, content);
	}
	
	@PreUpdate
	public void preUpdate(DBlink target) {
		String content = String.format("修改DBlink“%s”", target.getName());
		UserLogService.getInstance().save(UserLogOpt.MaintenanceDBlink, null, content);
	}
	
	@PreRemove
	public void preRemove(DBlink target) {
		String content = String.format("删除DBlink“%s”", target.getName());
		UserLogService.getInstance().save(UserLogOpt.MaintenanceDBlink, null, content);
	}
	
}
